/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.sunspotworld;

/**
 *
 * @author andresgodoysalavert
 */
import java.awt.*;

import java.awt.event.*;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.*;

import javax.swing.border.*;

 

public class LoginDialog extends JDialog {

 

    private JTextField tfUsername;
    private JPasswordField pfPassword;
    private JLabel lbUsername;
    private JLabel lbPassword;
    private JLabel imagelabel;
    private JButton btnLogin;
    private JButton btnOperador;
    private boolean succeeded;
 

    public LoginDialog(Frame parent) {

        super(parent, "Login", true);

        JPanel panel = new JPanel(new GridBagLayout());
        JPanel panel2 = new JPanel(new GridBagLayout());
        GridBagConstraints cs = new GridBagConstraints();
        
        ImageIcon image = new ImageIcon("C:\\Users\\Salaver\\Documents\\NetBeansProjects\\host\\trunk\\HidroponiaDesktop\\img\\logo.png");
        imagelabel = new JLabel(image);
        panel2.add(imagelabel);
        
        cs.fill = GridBagConstraints.HORIZONTAL;
        
        lbUsername = new JLabel("Usuario: ");
        cs.gridx = 0;
        cs.gridy = 8;
        cs.gridwidth = 1;
        panel.add(lbUsername, cs);
 
        tfUsername = new JTextField(20);
        cs.gridx =1;
        cs.gridy = 8;
        cs.gridwidth = 2;
        panel.add(tfUsername, cs);
 
        lbPassword = new JLabel("Clave: ");
        cs.gridx = 0;
        cs.gridy = 9;
        cs.gridwidth = 1;
        panel.add(lbPassword, cs);
 
        pfPassword = new JPasswordField(20);
        cs.gridx = 1;
        cs.gridy = 9;
        cs.gridwidth = 2;
        panel.add(pfPassword, cs);
        panel.setBorder(new LineBorder(Color.GRAY));
 
 
        btnLogin = new JButton("Login");
 
        btnLogin.addActionListener(new ActionListener() {
 
            public void actionPerformed(ActionEvent e) {
                try {
                    if (Login.authenticate(getUsername(), getPassword())) {
                        JOptionPane.showMessageDialog(LoginDialog.this,
                                "Bienvenido " + getUsername() + "! Has ingresado exitosamente.",
                                "Login",
                                JOptionPane.INFORMATION_MESSAGE);
                        succeeded = true;
                        dispose();
                        HidroponiaHost.cosecha.cargarCosecha();
                    } else {
                        JOptionPane.showMessageDialog(LoginDialog.this,
                                "Clave o Usuario invalido",
                                "Login",
                                JOptionPane.ERROR_MESSAGE);
                        // reset username and password
                        tfUsername.setText("");
                        pfPassword.setText("");
                        succeeded = false;
                    }
                } catch (SQLException ex) {
                    Logger.getLogger(LoginDialog.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
        btnOperador = new JButton("Operador");
        btnOperador.addActionListener(new ActionListener() {
 
            public void actionPerformed(ActionEvent e) {
                if (HidroponiaHost.cosecha.getId()>0){
                    dispose();
                    HidroponiaHost.usuario.setCargo("Operador");
                } else{
                    JOptionPane.showMessageDialog(LoginDialog.this,
                    "No hay cosechas creadas debe iniciar sesion primero como administrador!",
                    "Login",
                    JOptionPane.ERROR_MESSAGE);
                }
            }
        });
        JPanel bp = new JPanel();
        bp.add(btnLogin);
        bp.add(btnOperador);
 
        getContentPane().add(panel2, BorderLayout.PAGE_START);
        getContentPane().add(panel, BorderLayout.CENTER);
        getContentPane().add(bp, BorderLayout.PAGE_END);
        this.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
        pack();
        setResizable(false);
        setLocationRelativeTo(parent);
    }


 
    public String getUsername() {
        return tfUsername.getText().trim();
    }
 

    public String getPassword() {
        return new String(pfPassword.getPassword());
    }
 
    public boolean isSucceeded() {
        return succeeded;
    }
}
